﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class apikeysOutput : Api_BaseOutput
   {
      public List<apikeysResults> result { get; set; }
      public apikeysParms inputParms { get; private set; }
      // --------------------------------------------------------
      public apikeysOutput( apikeysInput input )
         : base( input )
      {
         this.result = new List<apikeysResults>();
         inputParms = input.parms;
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         if ( !this.inputParms.expired )
            return string.Format( "apikeys - active count: {0}", result.Count );
         else
         {
            int active = result.Where( r => r.expired_at == null || r.expired_at == DateTime.MinValue ).Count();
            return string.Format( "apikeys - total: {0}, active: {1}", result.Count, active );
         }
      }
      #region ================================================================== format setup
      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // --------------------------------------------------------------------
      // ---- xmlrpc
      internal void Format( x_apikeys[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }
      #endregion

      #region  ===================================================================== format result
      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( api_Response );
               api_ErrorMessages.Add( err );
            }
            else
            {
               x_apikeys[] jsonResult = jss.Deserialize<x_apikeys[]>( api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         DateTime dt;
         try
         {
            IEnumerable<XElement> apikeys = xml.Elements( "struct" );
            foreach ( XElement item in apikeys )
            {
               apikeysResults entry = new apikeysResults();
               entry.apikey = item.Element( "apikey" ).Value;
               DateTime.TryParse( item.Element( "created_at" ).Value, out dt ); entry.created_at = dt;
               DateTime.TryParse( item.Element( "expired_at" ).Value, out dt ); entry.expired_at = dt;
               //
               this.result.Add( entry );
            }
         }
         catch ( Exception ex )
         {
            api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------------------- FormatResults
      private List<apikeysResults> FormatResults( x_apikeys[] x_results )
      {
         List<apikeysResults> results = new List<apikeysResults>();
         //
         DateTime dt;
         foreach ( x_apikeys x in x_results )
         {
            apikeysResults entry = new apikeysResults();
            entry.apikey = x.apikey;
            DateTime.TryParse( x.created_at, out dt ); entry.created_at = dt;
            DateTime.TryParse( x.expired_at, out dt ); entry.expired_at = dt;
            results.Add( entry );
         }
         //
         return results;
      }
      // --------
      #endregion
   }
}
